<template>
  <div class="add-form">
    <!-- 添加表单 -->
    <el-form :model="userForm" ref="addForm" :rules="addFormRules" label-width="80px">
      <el-form-item label="用户名" prop="username" label-width="100px">
        <el-input v-model="userForm.username"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email" label-width="100px">
        <el-input v-model="userForm.email"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" label-width="100px" v-if="state">
        <el-input @blur="sendPwd" v-model="userForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="角色" label-width="100px">
        <el-input v-model="userForm.role"></el-input>
      </el-form-item>
      <el-form-item label="权限组名称" label-width="100px">
        <el-select v-model="userForm.permission_group_id">
          <el-option v-for="item in permissions" :key="item.id" :label="item.title" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" label-width="100px">
        <el-input v-model="userForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="介绍" label-width="100px">
        <el-input type="textarea" style="width:300px" placeholder="please input" v-model="userForm.introduction"></el-input>
      </el-form-item>
      <el-form-item label="" size="normal">
        <el-button @click="$emit('close')">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add, update } from '@/api/base/users'
export default {
  name: 'usersAdd',
  data() {
    return {
      // 表单校验规则
      addFormRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  props: {
    userForm: {
      type: Object,
      required: true
    },
    state: {
      type: Boolean,
      required: true
    },
    permissions: {
      type: Array,
      required: true
    }
  },
  created() {
    console.log(this.userForm)
  },
  methods: {
    //密码验证
    sendPwd: function() {
      var regPwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$/
      if (this.userForm.password != '' && !regPwd.test(this.userForm.password)) {
        this.$message({
          message: '密码必须是由4-20位字母+数字组合',
          type: 'error'
        })
      }
    },
    // 确定添加按钮
    submit() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        // 判断当前状态
        if (this.state) {
          // 添加状态
          try {
            await add(this.userForm)
            // console.log(res);
            this.$message.success('添加成功')
            this.$emit('close')
          } catch (error) {
            this.$message.error('添加失败')
          }
        } else {
          // 修改状态
          try {
            await update(this.userForm)
            this.$message.success('修改成功')
            this.$emit('close')
          } catch (error) {
            this.$message.success('修改失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.el-input {
  width: 300px;
}
</style>
